package Lianxi.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 修改密码
 */

public class Modify extends JPanel implements ActionListener {

    private JButton returnBut; //返回按钮
    private JButton confirm;   //确认按钮
    private JTextField modifyPwd;  //修改密码文本
    private JTextField confirmPwd;  //确认原密码文本
    private JTextField newPwd;    //确认新密码文本
    private Atm frame;
    int num = 3; //修改次数计数器

    public Modify(Atm frame) {

        this.frame = frame;

        //空布局
        this.setLayout(null);
        //设置透明
        this.setOpaque(false);

        //返回按钮,返回菜单界面
        returnBut = new JButton("返回");
        returnBut.setBounds(350, 600, 100, 50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认取款金额
        confirm = new JButton("确认");
        confirm.setBounds(350, 500, 100, 50);
        this.add(confirm);
        confirm.addActionListener(this);


        confirmPwd = new JTextField(); //确认旧密码文本框
        confirmPwd.setBounds(350, 220, 100, 30);
        this.add(confirmPwd);
        confirmPwd.addActionListener(this);

        JLabel label = new JLabel("确认旧密码："); //确认标签
        label.setBounds(230, 220, 80, 30);
        this.add(label);

        modifyPwd = new JTextField(); //修改密码文本框
        modifyPwd.setBounds(350, 270, 100, 30);
        this.add(modifyPwd);
        modifyPwd.addActionListener(this);

        JLabel label1 = new JLabel("输入新密码：");
        label1.setBounds(230, 270, 80, 30);
        this.add(label1);


        newPwd = new JTextField(); //确认新密码文本框
        newPwd.setBounds(350, 320, 100, 30);
        this.add(newPwd);
        newPwd.addActionListener(this);


        JLabel label2 = new JLabel("确认新密码：");
        label2.setBounds(230, 320, 80, 30);
        this.add(label2);


    }


    @Override
    public void actionPerformed(ActionEvent e) {

        String oldPwd = confirmPwd.getText(); //获取输入密码
        String nPwd = modifyPwd.getText(); //获取修改密码文本
        String affirm = newPwd.getText(); //获取确认新密码
        if (e.getSource() == returnBut) {
            frame.switchPanel("菜单");


        } else if (e.getSource() == confirm) {
            AtmSql sql = new AtmSql();
            User user = sql.getById(frame.getLoginUser().getId());


            sql.update(user,user);
            if (user.getPassword().equals(oldPwd)){  //数据库密码验证
                if (nPwd.equals(affirm)) {
                    user.setPassword(affirm);  // 确认修改密码
                    sql.update(user,user); //数据库保存
                    JOptionPane.showMessageDialog(this, "密码修改成功,请重新登录");
                    frame.switchPanel("登录");
                } else {
                    JOptionPane.showMessageDialog(this, "两次密码不一致，请重新输入");
                    num--; //错误次数自减
                }
            } else {
                JOptionPane.showMessageDialog(this, "原密码错误，请确认后再试");
                num--;
            }
            if (num == 0) {
                JOptionPane.showMessageDialog(this, "错误次数过多，请24小时后再试");
                System.exit(0); //强行退出
            }
        }

    }
}
